<? include("/inc/header.html") {} ?>

<div class="col-lg-12">
	<div class="panel panel" style="padding-top:5%;">
		<div class="panel-body pan" style="height: 333px">
			<div class="form-body pal" >
				<div style="padding-bottom:20px;">
					<label for="inputUsername" class="col-md-3 control-label">名称（只允许汉字、英文字母、数字及下划线,限制20个字符以内）<span
						class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<input id="name" type="text" class="form-control" />
						</div>
					</div>
				</div>
				<div>
					<label for="inputUsername" class="col-md-3 control-label">描述（20字以内）
						<span class='require'>*</span> </label>
					<div class="col-md-9">
						<div class="input-icon">
							<input id="description" type="text" class="form-control" />
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
var id = window.parent.getId();
	if(id != 0){
		$.ajax({
			type : "post",
			url : "knowledgeEditDataRoleEditQuery",
			dataType : "json",
			data:{
				"id":id,
			},
			beforeSend : function(XMLHttpRequest) {},
			success: function (data){  
           		 $("#name").val(data[0].name);
           		 $("#description").val(data[0].description);
    		}
		});
	}
	
	function save() {
		var name=$("#name").val(); //    
	    var re =/^[\u4e00-\u9fa5_a-zA-Z0-9]{1,20}$/;
		if (!re.test(name)) {
			layer.msg('名称:只允许汉字、英文字母、数字及下划线,限制20个字符以内。');
			return null;
		}
		
		if($("#description").val()!='' && $("#description").val().length>20){
			layer.msg('描述：20字以内');
			return null;
		}
	if(id == 0){
		
		
		if( $("#name").val()==""){
			layer.msg('请输入角色名！');
		}else if($("#description").val() ==""){
			layer.msg('请输入角色描述！');
		}else{ 
			$.ajax({
				type : "post",
				url : "knowledgeEditDataRoleAdd.do",
				dataType : "json",
				data:{
					"name":$("#name").val(),
					"description":$("#description").val()
				},
				beforeSend : function(XMLHttpRequest) {},
				success: function padlock(data) { 
					if(data[0].result=='success'){
						 var index = parent.layer.getFrameIndex(window.name);
	           		 	window.parent.queryRole();
	           		 	parent.layer.msg('添加成功');
	            	 	parent.layer.close(index); 
					}else{
						layer.msg(data[0].msg);
					}
	           		
	    		}
			});
		}
		}else{
		if( $("#name").val()==""){
			layer.msg('请输入角色名！');
		}else if($("#description").val() ==""){
			layer.msg('请输入角色描述！');
		}else{ 
			$.ajax({
				type : "post",
				url : "knowledgeEditDataRoleEdit.do",
				dataType : "json",
				data:{
					"id":id,
					"name":$("#name").val(),
					"description":$("#description").val()
				},
				beforeSend : function(XMLHttpRequest) {},
				success: function padlock(data) {  
					if(data[0].result=='success'){
						var index = parent.layer.getFrameIndex(window.name); 
	           		 	window.parent.queryRole();
	           		 	parent.layer.msg('编辑成功');
	            	 	parent.layer.close(index); 
					}else{
						layer.msg(data[0].msg);
					}
	    		}
			});
			}
		} 
	}
	
</script>
